import unittest
from boddle import boddle
import bottle

import restful_apis
import dao
import service


class TestRestfulApis(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()
        dao.create_all_tables()

    def test_get_users(self):
        sess = service.get_session()
        user = dao.User(name="sn")
        sess.add(user)
        user_sn = sess.query(dao.User).filter_by(name='sn').first()
        sess.commit()

        rest_user = restful_apis.query_users(str(user_sn.id))
        self.assertEqual(type(rest_user), dict)
        self.assertEqual(rest_user["id"], user_sn.id)
        self.assertEqual(rest_user["name"], user_sn.name)
        self.assertEqual(rest_user["role"], dao.UserRole.NORMAL_USER)

    def test_get_user_not_exists(self):
        try:
            restful_apis.query_users("100")
        except bottle.HTTPError as e:
            self.assertEqual(e.status_code, 404)
            return
        self.assertFalse(True, "Should not be here")
